package com.algorithm.base.greedy;

import org.springframework.stereotype.Service;

/**
 * 53. 最大子数组和
 *
 * @author Amadeus
 * @date 2023/11/16
 */
@Service
public class MaxSubArray {

    public int maxSubArray(int[] nums) {
        int result = nums[0];
        int now = 0;
        for (int i = 0; i < nums.length; i++) {
            now += nums[i];
            result = Math.max(result, now);
            if (now <= 0) {
                now = 0;
            }
        }
        return result;
    }

}
